#include "nonos.h"
#include <stdio.h>

#define EVT_BY_TIMER0 0x01
#define EVT_BY_TIMER1 0x02

int        cycle = 0;
nos_task_t task_evt;
nos_task_t task_timer0;
nos_task_t task_timer1;


static void task_func_timer0(void* args)
{
    static int cnt = 0;
    if (cnt == 1000) {
        cnt = 0;
        printf("%05d: timer0 set evt\n", cycle);
        nos_event_post(task_evt, EVT_BY_TIMER0);
        nos_task_delete(task_timer0);   // delete itself
        printf("%05d: timer0 deleted\n", cycle);
    }
    cnt += 10;
}
static void task_func_timer1(void* args)
{
    static int cnt = 0;
    if (cnt == 5000) {
        cnt = 0;
        printf("%05d: timer1 set evt\n", cycle);
        nos_event_post(task_evt, EVT_BY_TIMER1);
    }
    cnt += 10;
}
static void task_func_evt(nos_evmap_t events, void* args)
{
    if (events & EVT_BY_TIMER0) {
        printf("%05d: trigger by timer0\n", cycle);
    }
    if (events & EVT_BY_TIMER1) {
        printf("%05d: trigger by timer1\n", cycle);
    }
}

int nos_user_init(void)
{
    task_timer0 = nos_timer_task_create(task_func_timer0, nullptr, 10, 0);
    task_timer1 = nos_timer_task_create(task_func_timer1, nullptr, 10, 5);
    task_evt    = nos_event_task_create(task_func_evt, nullptr, EVT_BY_TIMER0 | EVT_BY_TIMER1);

    return 0;
}

#define TEST_TICK_NUM 60 * 1000

int main()
{
    for (cycle = 0; cycle < TEST_TICK_NUM; cycle++) {
        nos_systick();
        nos_main();
    }
}
